package de.stefanocke.roo.wicket.templates

import de.stefanocke.roo.wicket.Stereotypes

class TableTemplate extends JavaHtmlTemplate{
	
	override String javaClassNameWhenNew(){
		"TablePanel"
	}
	
	override String stereotype(){
		Stereotypes::TABLE
	}
	
	override subPackageToUseWhenNew(){
		"common"
	}
		
	override buildJavaContents(){
		import_("java.util.List")
		import_("org.apache.wicket.extensions.ajax.markup.html.repeater.data.table.AjaxFallbackDefaultDataTable")
		import_("org.apache.wicket.extensions.markup.html.repeater.data.grid.ICellPopulator")
		import_("org.apache.wicket.extensions.markup.html.repeater.data.table.AbstractColumn")
		import_("org.apache.wicket.extensions.markup.html.repeater.data.table.DataTable")
		import_("org.apache.wicket.extensions.markup.html.repeater.data.table.IColumn")
		import_("org.apache.wicket.extensions.markup.html.repeater.data.table.ISortableDataProvider")
		import_("org.apache.wicket.extensions.markup.html.repeater.data.table.PropertyColumn")
		import_("org.apache.wicket.markup.html.panel.Panel")
		import_("org.apache.wicket.markup.repeater.Item")
		import_("org.apache.wicket.model.PropertyModel")
		import_("org.apache.wicket.model.ResourceModel")
		import_("org.apache.wicket.model.IModel")

		val ComponentFactory = import_(typeof(ComponentFactoryTemplate).generatedJavaType)
		
		'''	
		«stereotypeAnnotation»
		public abstract class «simpleTypeName»<T> extends Panel {
			private static final long serialVersionUID = 1L;
		
			protected «simpleTypeName»() {
				super("INVALID");
				throw new UnsupportedOperationException("Do not use this constructor since it is only a workaround for ITD issues");
			}
			
			public «simpleTypeName»(String id, ISortableDataProvider<T> dataProvider) {
				super(id);
				//wildcard is workaround for ITD issue https://bugs.eclipse.org/bugs/show_bug.cgi?id=382435
				List<?> colums = createColumns();
				@SuppressWarnings("unchecked")
				DataTable<T> table = createTable("table", (List<IColumn<T>>) colums, dataProvider);
				add(table);
			}
		
			protected DataTable<T> createTable(String id, List<IColumn<T>> columns, ISortableDataProvider<T> dataProvider) {
				return new AjaxFallbackDefaultDataTable<T>(id, columns, dataProvider, getRowsPerPage());
			}
		
			protected int getRowsPerPage() {
				return 10;
			}
		
			protected abstract List<?> createColumns();
		
		
			protected IColumn<T> createColumn(String propertyExpression, String resourceKey, String sortProperty) {
				return new PropertyColumn<T>(new ResourceModel(resourceKey),
						sortProperty, propertyExpression){
							
					private static final long serialVersionUID = 1L;
					
					@Override
					public boolean isSortable() {
						return «simpleTypeName».this.isSortable() && super.isSortable();
					}
				};
			}
			
			@SuppressWarnings("serial")
			protected <M> IColumn<T> createColumn(final String propertyExpression, String resourceKey, String sortProperty, 
				final «ComponentFactory»<M> cf) {
				return new AbstractColumn<T>(new ResourceModel(resourceKey), sortProperty) {
					@Override
					public void populateItem(Item<ICellPopulator<T>> cellItem, String componentId, IModel<T> rowModel) {
						cellItem.add(cf.createComponent(componentId, new PropertyModel<M>(rowModel, propertyExpression)));
						
					}
					@Override
					public boolean isSortable() {
						return TablePanel.this.isSortable() && super.isSortable();
					}
				};
			}
			
			protected boolean isSortable(){
				return false;
			}
		}
		'''
	}
	
	override buildHtmlBody(){
		'''
		<wicket:panel>
			<table wicket:id="table" class="dataview"/>
		</wicket:panel>
		'''
	}
}